package com.ruoyi.logistic.service.impl;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.domain.vo.TableVo;
import com.ruoyi.logistic.domain.*;
import com.ruoyi.logistic.domain.vo.NetworkClientGoRankVo;
import com.ruoyi.logistic.domain.vo.NetworkClientGoVo;
import com.ruoyi.logistic.enums.NetworkClientGoDetailEnum;
import com.ruoyi.logistic.mapper.*;
import com.ruoyi.logistic.service.NetworkClientGoService;
import com.ruoyi.qualityControl.domain.vo.QcFalseSignComplainEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@Service
public class NetworkClientGoServiceImpl implements NetworkClientGoService {
    @Autowired
    private NetworkClientGoDetailMapper networkClientGoDetailMapper;
    @Autowired
    private NetworkClientGoCollectMapper networkClientGoCollectMapper;
    @Autowired
    private NetworkClientGoProvinceIncomeRankMapper networkClientGoProvinceIncomeRankMapper;
    @Autowired
    private NetworkClientGoClientIncomeRankMapper networkClientGoClientIncomeRankMapper;
    @Autowired
    private NetworkNameMapper networkNameMapper;
    @Autowired
    private NetworkClientMapper networkClientMapper;
    @Override
    public List<NetworkClientGoDetail> selectAll(PageDate pageDate) {
        PageHelper.startPage(pageDate);
        return networkClientGoDetailMapper.selectAll();
    }

    @Override
    public Boolean insert(NetworkClientGoDetail networkClientGoDetail) {
        return networkClientGoDetailMapper.insert(networkClientGoDetail);
    }

    @Override
    public Boolean update(NetworkClientGoDetail networkClientGoDetail) {
        return networkClientGoDetailMapper.update(networkClientGoDetail);
    }

    @Override
    public Boolean deleteById(Integer id) {
        return networkClientGoDetailMapper.deleteById(id);
    }

    @Override
    public List<NetworkClientGoDetail> search(NetworkClientGoVo networkClientGoVo) {
        PageHelper.startPage(networkClientGoVo.getPageDate());
        return networkClientGoDetailMapper.search(networkClientGoVo);
    }

    @Override
    public List<TableVo> initTable() {
        List<TableVo> list = new ArrayList<>();
        TableVo tableVo = new TableVo();
        String[] fields = {
                "networkNameId","clientId","receiveSiteNameId","destProvinceName","totalPercent","weightPercent",
                "incomePercent","total","totalRealWeight","avgWeight","income","kgZeroTotal",
                "kgZeroIncome","kgZeroToThreeTenthTotal","kgZeroToThreeTenthWeight","kgZeroToThreeTenthAvgWeight","kgZeroToThreeTenthIncome","kgThreeTenthToFiveTenthTotal",
                "kgThreeTenthToFiveTenthWeight","kgThreeTenthToFiveTenthAvgWeight","kgThreeTenthToFiveTenthIncome","kgFiveTenthToOneTotal","kgFiveTenthToOneWeight","kgFiveTenthToOneAvgWeight",
                "kgFiveTenthToOneIncome","kgOneToOneDotFiveTotal","kgOneToOneDotFiveWeight","kgOneToOneDotFiveAvgWeight","kgOneToOneDotFiveIncome","kgOneDotFiveToTwoTotal",
                "kgOneDotFiveToTwoWeight","kgOneDotFiveToTwoAvgWeight","kgOneDotFiveToTwoIncome","kgTwoToTwoDotFiveTotal","kgTwoToTwoDotFiveWeight","kgTwoToTwoDotFiveAvgWeight",
                "kgTwoToTwoDotFiveIncome","kgTwoDotFiveToThreeTotal","kgTwoDotFiveToThreeWeight","kgTwoDotFiveToThreeAvgWeight","kgTwoDotFiveToThreeIncome","kgThreeToFiveTotal",
                "kgThreeToFiveWeight","kgThreeToFiveAvgWeight","kgThreeToFiveIncome","kgFiveToTenTotal","kgFiveToTenWeight","kgFiveToTenAvgWeight",
                "kgFiveToTenIncome","kgTenToThirtyTotal","kgTenToThirtyWeight","kgTenToThirtyAvgWeight","kgTenToThirtyIncome","kgThirtyToFiftyTotal",
                "kgThirtyToFiftyWeight","kgThirtyToFiftyAvgWeight","kgThirtyToFiftyIncome","kgOverFiftyTotal","kgOverFiftyWeight","kgOverFiftyAvgWeight",
                "kgOverFiftyIncome","createTime","updateTime"
               };
        for (String field : fields) {
            tableVo = new TableVo();
            tableVo.setFields(field);
            list.add(tableVo);
        }
        for (TableVo vo : list) {
            vo.setTranslate(NetworkClientGoDetailEnum.getValue(vo.getFields()));
        }
        return list;
    }

    @Override
    public List<NetworkClientGoCollect> clientGoCollect(NetworkClientGoVo networkClientGoVo) {
        PageHelper.startPage(networkClientGoVo.getPageDate());
        List<NetworkClientGoCollect> collects = networkClientGoCollectMapper.clientGoCollect(networkClientGoVo);
        for (NetworkClientGoCollect collect : collects){
            collect.setNetworkName(networkNameMapper.selectNameById(collect.getNetworkNameId()));
            collect.setClientName(networkClientMapper.selectNameById(collect.getClientId()));
        }
        return collects;
    }

    @Override
    public List<NetworkClientGoProvinceIncomeRank> provinceIncomeRank(NetworkClientGoRankVo networkClientGoRankVo) {
        return networkClientGoProvinceIncomeRankMapper.provinceIncomeRank(networkClientGoRankVo);
    }

    @Override
    public NetworkClientGoClientIncomeRankAll clientIncomeRank(NetworkClientGoRankVo networkClientGoRankVo) {
       List<NetworkClientGoClientIncomeRank> list = networkClientGoClientIncomeRankMapper.clientIncomeRank(networkClientGoRankVo);
        List<String> clientNameList = new ArrayList<>();
        List<BigDecimal> incomeList = new ArrayList<>();

        for (NetworkClientGoClientIncomeRank obj : list) {
            clientNameList.add(obj.getClientId());
            incomeList.add(obj.getIncome());
        }

// 然后将集合转换为数组
        String[] clientNames = clientNameList.toArray(new String[0]);
        BigDecimal[] incomes = incomeList.toArray(new BigDecimal[0]);

// 创建并初始化新对象
        NetworkClientGoClientIncomeRankAll all = new NetworkClientGoClientIncomeRankAll();
        all.setClientName(clientNames);
        all.setIncome(incomes);
        return all;
    }
}
